﻿#include "HourGlassWidget.h"

#include <QPainter>
#include <QBrush>
#include <QPen>

HourGlassWidget::HourGlassWidget(QWidget *parent)
    : QWidget(parent)
    , timer_(new QTimer(this))
    , sandAmount_(0.0)
    , isRuning_(false)
    , totalTime_(60)
{
    connect(timer_, &QTimer::timeout, this, &HourGlassWidget::updateSand);
    timer_->start(50);
    elapsedTimer_.start();
    isRuning_ = true;
}

HourGlassWidget::~HourGlassWidget()
{

}

void HourGlassWidget::start()
{
    elapsedTimer_.start();
    isRuning_ = true;
}

void HourGlassWidget::stop()
{
    isRuning_ = false;
}

void HourGlassWidget::reset()
{
    sandAmount_ = 0;
    isRuning_ = false;
    update();
}

void HourGlassWidget::paintEvent(QPaintEvent *event)
{
    Q_UNUSED(event);
    QPainter painter(this);
    painter.setRenderHint(QPainter::Antialiasing);

    int width = this->width();
    int height = this->height();

    int centerX = width / 2;

    // 绘制沙漏外框

    painter.setPen(QPen(Qt::black, 2));
    painter.setBrush(Qt::NoBrush);

    // 上部分玻璃容器
    QPainterPath topGlass;
    topGlass.moveTo(centerX - 50, 50);
    topGlass.lineTo(centerX - 30, height / 2 - 20);
    topGlass.lineTo(centerX + 30, height / 2 - 20);
    topGlass.lineTo(centerX + 50, 50);
    topGlass.lineTo(centerX - 50, 50);
    painter.drawPath(topGlass);


    // 下部分玻璃容器
    QPainterPath bottomGlass;
    bottomGlass.moveTo(centerX - 50, height - 50);
    bottomGlass.lineTo(centerX - 30, height / 2 + 20);
    bottomGlass.lineTo(centerX + 30, height / 2 + 20);
    bottomGlass.lineTo(centerX + 50, height - 50);
    bottomGlass.lineTo(centerX - 50, height - 50);
    painter.drawPath(bottomGlass);

    // 绘制中间连接部分
    painter.drawLine(centerX - 20, height / 2 - 20, centerX - 20, height / 2 + 20);
    painter.drawLine(centerX + 20, height / 2 - 20, centerX + 20, height / 2 + 20);

    // 绘制沙子
    painter.setBrush(QBrush(QColor(194, 178, 128)));

    // 上部分沙子
    QPainterPath topSand;
    topSand.moveTo(centerX - 30, height / 2 - 20);
    topSand.lineTo(centerX + 30, height / 2 - 20);
    topSand.lineTo(centerX + 30 + 20 * (1.0 - sandAmount_), 50 + (height / 2 - 70) * sandAmount_);
    topSand.lineTo(centerX - 30 - 20 * (1.0 - sandAmount_), 50 + (height / 2 - 70) * sandAmount_);
    painter.drawPath(topSand);

    // 下部分沙子
    QPainterPath bottomSand;
    bottomSand.moveTo(centerX - 50, height - 50);
    bottomSand.lineTo(centerX + 50, height - 50);
    bottomSand.lineTo(centerX + 30 + 20 * (1.0 - sandAmount_), height - 50 - (height/2 - 70) * sandAmount_);
    bottomSand.lineTo(centerX - 30 - 20 * (1.0 - sandAmount_), height - 50 - (height/2 - 70) * sandAmount_);
    painter.drawPath(bottomSand);

    // 流动中的沙子
    if (sandAmount_ > 0 && sandAmount_ < 1) {
        QPainterPath flowingSand;
        flowingSand.moveTo(centerX - 20, height / 2 - 20);
        flowingSand.lineTo(centerX - 20, height / 2 + 20);
        flowingSand.lineTo(centerX + 20, height / 2 + 20);
        flowingSand.lineTo(centerX + 20, height / 2 - 20);
        painter.drawPath(flowingSand);
    }

    // 显示剩余时间
    int remaining = totalTime_ * (1 - sandAmount_);
    painter.drawText(rect(), Qt::AlignCenter, QString::number(remaining) + "s");
}

void HourGlassWidget::updateSand()
{
    if(!isRuning_)
        return;


    qint64 elapsed = elapsedTimer_.elapsed();

    sandAmount_ = qMin(1.0 , elapsed / (totalTime_ * 1000.0));

    update();

    if(sandAmount_ >= 1.0){
        isRuning_ = false;
    }
}
